<?php
/* clean the post data */
function clean($data) {
	if(is_array($data)) {
		foreach ($data as $key => $value) {
			$data[$key] = clean($value);
		}
	} else $data = htmlspecialchars($data, ENT_QUOTES, 'UTF-8');
	return $data;
}

/* form validation */
function check_time($hh, $mn) {
	if($hh<=24 && $hh>=0) {
		if($mn<=60 && $mn>= 0) {
			return true;
		} else return false;
	} else return false;
}
function check_email ($email) {
	$verif = "!^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]{2,}\.[a-zA-Z]{2,5}$!";
	if(preg_match($verif,$email) ) {
		return true;
	} else return false;
}
function check_gender ($gender) {
	if($gender == "MALE" || $gender == "FEMALE") return true;
	else return false;
}
function check_password($py) {
	$verif = "!^\w{5,20}$!";
	if(preg_match($verif,$py) ) {
		return true;
	} else return false;
}
function check_phoneNumber($phoneNumber) {
	$verif = '`^([0-9]{10})$`';
	if(preg_match($verif,$phoneNumber) ) {
		return true;
	} else return false;
}
function check_userexist($email) {
	$request_check = "SELECT * FROM `T_PERSON` WHERE `EMAIL_ADDRESS` = '".$email."'";
	$result_check = mysql_query($request_check);
	if(!mysql_num_rows($result_check)) 
		return true;
	else return false;
}

function check_db_password($post_pass, $db_pass) {
	if(!empty($post_pass) && !empty($db_pass)){
		$post_pass = md5($post_pass);
		if($post_pass == $db_pass) return true;
		else return false;
	} else return false;
}

/* display functions */
function num_to_fr_months_abb($num){
	switch ($num) {
		case 1:
			echo "Janv.";
			break;
		case 2:
			echo "Févr.";
			break;
		case 3:
			echo "Mars.";
			break;
		case 4:
			echo "Avr.";
			break;
		case 5:
			echo "Mai.";
			break;
		case 6:
			echo "Juin.";
			break;
		case 7:
			echo "Juil.";
			break;
		case 8:
			echo "Août.";
			break;
		case 9:
			echo "Sept.";
			break;
		case 10:
			echo "Oct.";
			break;
		case 11:
			echo "Nov.";
			break;
		case 12:
			echo "Déc.";
			break;
		default:
			break;
	}
}
function num_to_fr_months($num){
	switch ($num) {
		case 1:
			echo "Janvier.";
			break;
		case 2:
			echo "Février";
			break;
		case 3:
			echo "Mars";
			break;
		case 4:
			echo "Avril";
			break;
		case 5:
			echo "Mai";
			break;
		case 6:
			echo "Juin";
			break;
		case 7:
			echo "Juillet";
			break;
		case 8:
			echo "Août";
			break;
		case 9:
			echo "Septembre";
			break;
		case 10:
			echo "Octobre";
			break;
		case 11:
			echo "Novembre";
			break;
		case 12:
			echo "Décembre";
			break;
		default:
			break;
	}
}
function display_select_options_minutes(){
	for($i=0;$i<59;$i++){
		$ii = str_pad($i,2,"0",STR_PAD_LEFT);
		echo('<a class="select_option" val="'.$ii.'">'.$ii.'<span class="selectable_opacity"></span></a>');
	}
}
function display_select_options_hours(){
	for($i=0;$i<24;$i++){
		$ii = str_pad($i,2,"0",STR_PAD_LEFT);
		echo('<a class="select_option" val="'.$ii.'">'.$ii.'<span class="selectable_opacity"></span></a>');
	}
}
function display_select_options_years_birth(){
	$date = getdate();
	for($i=$date['year']; $i > 1940; $i--){
		echo('<a class="select_option" val="'.$i.'">'.$i.'<span class="selectable_opacity"></span></a>');
	}
}
function display_select_options_years_wedding(){
	$date = getdate();
	for($i=2012; $i <= $date['year']+5; $i++){
		echo('<a class="select_option" val="'.$i.'">'.$i.'<span class="selectable_opacity"></span></a>');
	}
}
function display_select_options_months(){
	for($i=1;$i<=12;$i++){
		$ii = str_pad($i,2,"0",STR_PAD_LEFT);
		echo('<a class="select_option" val="'.$ii.'">');
		num_to_fr_months_abb($i);
		echo('<span class="selectable_opacity"></span></a>');
	}
}
function display_select_options_days(){
	for($i=1;$i<=31;$i++){
		$ii = str_pad($i,2,"0",STR_PAD_LEFT);
		echo('<a class="select_option" val="'.$ii.'">'.$ii.'<span class="selectable_opacity"></span></a>');
	}
}

function read_file($url, $use = 'FOP') {
	$url = urldecode($url);
	switch ($use) {
		case 'FOP':
			$handle = fopen($url, 'r');
			while (!feof($handle)) {
				$content.=fread($handle, 2048);
			}
			return $content;
			break;
			
		case 'FGC':
			if(!$handle = file_get_contents($url)) { 
				return false;
			} else {
				while($chunk = fread($handle, 2048)) {
					$content .= $chunk;
				}
				fclose($handle);
				return $content;
			}
			break;
			
		case 'CURL':
			if(function_exists('curl_init')) {
				$ch = curl_init();
				curl_setopt($ch, CURLOPT_URL, $url);
				curl_setopt($ch, CURLOPT_HEADER, false);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
				curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
				$tmp = curl_exec($ch);
				if($tmp == false) return 'ERROR N°'.curl_errno($ch).': '.curl_error($ch);
				curl_close($ch);
				return trim($tmp);
			} else return 'curl library is not installed';
			break;
	}
}

function filter($in) {
	$search = array ('@[éèêëÊË]@i','@[àâäÂÄ]@i','@[îïÎÏ]@i','@[ûùüÛÜ]@i','@[ôöÔÖ]@i','@[ç]@i','@[ ]@i');
	$replace = array ('e','a','i','u','o','c','_');
	return preg_replace($search, $replace, $in);
}

function generate_guid($length = 10) {     
    $chars = '2bc3df4gh5jk6lm7np8rs9tv1wxzaeiou';
    $result = '';
	for($p = 0; $p<$length; $p++){
        $result .= ($p%2) ? $chars[mt_rand(19, 23)] : $chars[mt_rand(0, 18)];
    }
    return $result;
}

/* Sending email functions */
function smtp_mail_register($sendto_email, $subject, $body, $from, $user_name){
	$subject = $subject;
	$message = "Hello! This is a simple email message.<br />
						key:<a href='http://www.promessedunevie.com/~developer2/wedding_admin/inc/userActive.php?key=$body'>http://www.promessedunevie.com/~developer2/wedding_admin/inc/userActive.php?key=$body</a>";
	$headers = "From: $user_name <$from>" . "\r\n" .
					'Reply-To: noreply@promessedunevie.com' . "\r\n" .
					'MIME-Version: 1.0r'."\r\n" .
					'Content-type: text/html'."\r\n";
	if(mail($sendto_email,$subject,$message,$headers))
		return true;
	else return false;
}
function smtp_mail_forgetPW($sendto_email, $subject, $body, $from, $user_name){
	$subject = $subject;
	$message = "Hello! This is a simple email message.<br />
						Your new password: $body";
	$headers = "From: $user_name <$from>" . "\r\n" .
					'Reply-To: noreply@promessedunevie.com' . "\r\n" .
					'MIME-Version: 1.0r'."\r\n" .
					'Content-type: text/html'."\r\n";
	if(mail($sendto_email,$subject,$message,$headers))
		return true;
	else return false;
}
function smtp_mail_invite($sendto_email, $subject, $body, $from, $user_name){
	$subject = $subject;
	$message = "Hello! This is a simple email message.<br />
						$body";
	$headers = "From: $user_name <$from>" . "\r\n" ;
	$headers .=	"Reply-To: noreply@promessedunevie.com" . "\r\n" .
					'MIME-Version: 1.0r'."\r\n";
	$headers .= 'Content-Type: text/html; charset="UTF-8"' . "\n"; 
	$headers .= 'Content-Transfer-Encoding: 8bit';
	
	$message_html  = "<html>\n";
	$message_html .= "<body>\n";
	$message_html .= utf8_decode(html_entity_decode(stripslashes($message), ENT_QUOTES, 'UTF-8'))."\n";
	$message_html .= "</body>\n";
	$message_html .= "</html>\n";
	
	if(mail($sendto_email,$subject,$message,$headers))
		return true;
	else return false;
}

function http_build_headers($headers) {
	$headers_brut = '';
	foreach($headers as $nom => $valeur) {
		$headers_brut .= $nom . ': ' . $valeur . "\r\n";
	}
	return $headers_brut;
}

function getRecommand($guid) {
	$db = Samsung_Db_Oracle::getConnection();
	$result = -1;
	
	try {
		$statement = $db->execute("
			select SUA_MEMBER_RECOMMAND from SAM_SUA_MEMBER where SUA_MEMBER_ID = ?
		", $guid);
		
		while($row = $statement->fetch()) {	
			$result = $row['SUA_MEMBER_RECOMMAND'];
		}
		return $result;
	} catch(Exception $ex) {
		writeLog($ex->getMessage());
		return $result;
	}
}

function writeLog($errorText) {
	$fp = fopen("../logs.txt", 'a+');
	$textLog = date('Ymd - G:i:s')." : ".$errorText." \r\n";
	fwrite ($fp, $textLog);
	fclose($fp);
}

?>